package com.javer.domain;

import java.io.Serializable;
import java.util.List;

public class PageView<T> implements Serializable {
	private static final long serialVersionUID = 1L;
	
	public static final int PAGE_SIZE = 20;
	
	private int pageNo;
	
	private int count;
	
	private int pageSize;
	
	private int pageCount;
	
	private PageIndex pageIndex;
	
	private List<T> records;
	
	private String tableName;
	
	public PageView() {
		
	}
	
	public PageView(int pageNo, int pageSize) {
		this.pageNo = pageNo;
		this.pageSize = pageSize;
		pageIndex = new PageIndex(pageNo - PageIndex.LEN / 2, pageNo + PageIndex.LEN / 2);
	}
	
	public PageView(int pageNo, int pageSize, String tableName) {
		this.pageNo = pageNo;
		this.pageSize = pageSize;
		this.tableName = tableName;
		pageIndex = new PageIndex(pageNo - PageIndex.LEN / 2, pageNo + PageIndex.LEN / 2);
	}

	public PageView(int pageNo, int pageSize, int pageCount, int count) {
		this.pageNo = pageNo;
		this.count = count;
		this.pageSize = pageSize;
		this.pageCount = pageCount;
		pageIndex = new PageIndex(pageNo - PageIndex.LEN / 2, pageNo + PageIndex.LEN / 2);
	}
	
	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public PageIndex getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(PageIndex pageIndex) {
		this.pageIndex = pageIndex;
	}

	public int getPageNo() {
		return pageNo;
	}

	public PageView setPageNo(int pageNo) {
		this.pageNo = pageNo;
		return this;
	}

	public int getCount() {
		return count;
	}

	public PageView setCount(int count) {
		this.count = count;
		return this;
	}

	public int getPageSize() {
		return pageSize;
	}

	public PageView setPageSize(int pageSize) {
		this.pageSize = pageSize;
		return this;
	}

	public int getPageCount() {
		return pageCount;
	}

	public PageView setPageCount(int pageCount) {
		this.pageCount = pageCount;
		if (pageIndex.getBegin() <= 0) {
			pageIndex.setBegin(1);
			pageIndex.setEnd(pageIndex.getBegin() + PageIndex.LEN);
			if (pageIndex.getEnd() > pageCount) {
				pageIndex.setEnd(pageCount);
			}
		}
		else if (pageIndex.getEnd() > pageCount) {
			pageIndex.setEnd(pageCount);
			pageIndex.setBegin(pageCount - PageIndex.LEN);
			if (pageIndex.getBegin() <= 0) {
				pageIndex.setBegin(1);
			}
		}
		return this;
	}

	public List<T> getRecords() {
		return records;
	}

	public PageView setRecords(List<T> records) {
		this.records = records;
		return this;
	}

	@Override
	public String toString() {
		return "PageView [count=" + count + ", pageCount=" + pageCount
				+ ", pageNo=" + pageNo + ", pageSize=" + pageSize + "]";
	}

}
